﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.COMM;
using Mang24k99.Business.COMM;
using Wuqi.Webdiyer;

namespace Mang24k99.Web.COMM
{
    public partial class KtCommCodeList : System.Web.UI.Page
    {
        string _AspNetName = "CustomInfoHTML";

        public string AspNetCustomInfoHTML
        {
            get
            {
                return Common.ViewStateHelper.GetString(this.ViewState, _AspNetName);
            }
            set
            {
                Common.ViewStateHelper.SetViewState(this.ViewState, value, _AspNetName);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //this.btnCancel.Attributes.Add("onclick", "return Cancel()");
            if (!IsPostBack)
            {
                this.AspNetCustomInfoHTML = this.AspNetPager1.CustomInfoHTML;
                MKtCommCodeList.SelectAll();
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                BindDDL();
                BindAspNetPager();
                BinDate();
            }

        }

        

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="type"></param>
        public void InItDate(int codeno)
        {
            MKtCommCodeList codelist = MKtCommCodeList.Select(codeno);
            this.txtCName.Text = codelist.CODE_CNAME;
            this.txtcode.Text = codelist.CODE_NO.ToString();
            this.txtEName.Text = codelist.CODE_ENAME;
            this.txtRemark.Text = codelist.CODE_COMMENTS;
            this.ddlIsValidate.SelectedValue = codelist.IS_VALIDATE.ToString();
            this.ddlTypecode.SelectedValue = codelist.MKtCommCodeType.TYPE_CODE.ToString();
        }
        /// <summary>
        /// 清空页面
        /// </summary>
        public void ClearPage()
        {
            this.txtEName.Text = "";
            this.txtCName.Text = "";
            this.txtcode.Text = "";
            this.txtRemark.Text = "";
            this.txtTypeCode.Text = "";
            this.ddlIsValidate.SelectedValue = "1";
        }

        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
           
            //Insert为新增
            if (Session["Action"].ToString()=="Insert")
            {
                if (CodeValidate()==false)
                {
                    return;
                }
                if (CNameValidate()==false)
                {
                    return;
                }
                if (ENameValidate()==false)
                {
                    return;
                }
                MKtCommCodeType mcode = new MKtCommCodeType();
                mcode.TYPE_CODE = Int32.Parse(ddlTypecode.SelectedValue);
                MKtCommCodeList codelist = new MKtCommCodeList();
                codelist.CODE_NO = Int32.Parse(txtcode.Text.Trim());
                codelist.CODE_ENAME = txtEName.Text.Trim();
                codelist.CODE_CNAME = txtCName.Text.Trim();
                codelist.CODE_COMMENTS = txtRemark.Text;
                codelist.MKtCommCodeType = mcode;
                codelist.IS_VALIDATE = Int32.Parse(ddlIsValidate.SelectedValue);
                codelist.INPUT_TIME = DateTime.Now;
                bool result = BKtCommCodeList.Save(codelist);
                if (result == true)
                {
                    string strMsg = "新增成功!";
                    Common.MessageBox.Show(this.Page, strMsg);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    ClearPage();
                    BindAspNetPager();
                    BinDate();
                }
                else
                {
                    string strMsg = "新增失败！";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
            }
            //Update为编辑操作
            if (Session["Action"].ToString()=="Update")
            {
                if (txtcode.Text.Trim()!=Session["Code"].ToString())
                {
                    if (CodeValidate()==false)
                    {
                        return;
                    }
                }
                if (txtCName.Text.Trim()!=Session["CName"].ToString())
                {
                    if (CNameValidate()==false)
                    {
                        return;
                    }
                }
                if (ENameValidate()==false)
                {
                    return;
                }
                MKtCommCodeType mcode = new MKtCommCodeType();
                mcode.TYPE_CODE = Int32.Parse(ddlTypecode.SelectedValue);
                MKtCommCodeList codelist = new MKtCommCodeList();
                codelist.CODE_NO = Int32.Parse(txtcode.Text.Trim());
                codelist.CODE_ENAME = txtEName.Text.Trim();
                codelist.CODE_CNAME = txtCName.Text.Trim();
                codelist.CODE_COMMENTS = txtRemark.Text;
                codelist.MKtCommCodeType = mcode;
                codelist.IS_VALIDATE = Int32.Parse(ddlIsValidate.SelectedValue);
                codelist.MODIFY_TIME = DateTime.Now;
                codelist.ID = Int32.Parse(Session["Code"].ToString());
                bool result = BKtCommCodeList.Update(codelist);

                if (result == true)
                {
                    string strMsg = "更新成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    this.txtcode.ReadOnly = false;
                    ClearPage();
                    BindAspNetPager();
                    BinDate();  
                }
                else
                {
                    string strMsg = "更新失败！";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
            }
        }

        /// <summary>
        /// 绑定类型编码
        /// </summary>
        public void BindDDL()
        {
            try
            {
                MKtCommCodeType[] codetype = MKtCommCodeType.SelectAll();
                this.ddlTypecode.DataSource = codetype;
                this.ddlTypecode.DataTextField = "TYPE_NAME";
                this.ddlTypecode.DataValueField = "TYPE_CODE";
                this.ddlTypecode.DataBind();
            }
            catch (System.Exception e)
            {
            	
            }
           
        }

        /// <summary>
        /// 转换是否有效字段的字符串格式
        /// </summary>
        /// <param name="isValidate"></param>
        /// <returns></returns>
        public string GetStatus(int isValidate)
        {
            if (isValidate == 1)
            {
                return "使用";
            }
            else
            {
                return "禁用";
            }
        }
       
        /// <summary>
        /// 转换类型编码显示格式
        /// </summary>
        /// <param name="typecode"></param>
        /// <returns></returns>
        public string GetTypeName(int typecode)
        {
            return BKtCommCodeList.GetTypeName(typecode);
        }
       
        /// <summary>
        /// 选择gridview中某一行进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.grdList.SelectedIndex;
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            if (grdList.Rows[index].Cells[2].Text != "")
            {
                this.txtcode.ReadOnly = true;
                this.txtcode.Text = grdList.Rows[index].Cells[2].Text;
                Session["Code"] = grdList.Rows[index].Cells[2].Text;
            }
            else
            {
                this.txtcode.Text = "";
            }
            if (grdList.Rows[index].Cells[3].Text != "")
            {
                this.txtCName.Text = grdList.Rows[index].Cells[3].Text;
                Session["CName"] = grdList.Rows[index].Cells[3].Text;
            }
            else
            {
                this.txtCName.Text = "";
            }
            if (grdList.Rows[index].Cells[4].Text != "")
            {
                this.txtEName.Text = grdList.Rows[index].Cells[4].Text;
            }
            else
            {
                this.txtEName.Text = "";
            }

            if (grdList.Rows[index].Cells[7].Text != "" && grdList.Rows[index].Cells[7].Text!="&nbsp;")
            {
                this.txtRemark.Text = grdList.Rows[index].Cells[7].Text;
            }
            else
            {
                this.txtRemark.Text = "";
            }
            GridViewRow gvr = grdList.Rows[index];
            Label lab = (Label)gvr.FindControl("lab");
            Label labtype = (Label)gvr.FindControl("labtype");
            this.ddlTypecode.ClearSelection();
            this.ddlTypecode.Items.FindByText(labtype.Text).Selected = true;
            this.ddlIsValidate.ClearSelection();
            this.ddlIsValidate.Items.FindByText(lab.Text).Selected = true;
            Session["Action"] = "Update";
        }
      
        /// <summary>
        /// 删除操作(物理删除)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MKtCommCodeList codelist = new MKtCommCodeList();
            string codeno = this.grdList.Rows[e.RowIndex].Cells[2].Text;
            codelist = MKtCommCodeList.Select(Int32.Parse(codeno));
            Boolean result = BKtCommCodeList.Delete(codelist);
            if (result == true)
            {
                string strMsg = "删除成功！";
                Common.MessageBox.Show(this.Page, strMsg);
                BindAspNetPager();
                BinDate();
            }
            else
            {
                string strMsg = "删除失败！";
                Common.MessageBox.Show(this.Page, strMsg);
            }
        }

        /// <summary>
        /// 绑定grdList
        /// </summary>
        public void BinDate()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int maxRecord = aspNetPager.PageSize;
            int startRecord = aspNetPager.StartRecordIndex - 1;
            string typeName = this.txtTypeCode.Text.Trim();
            object dataSource = BKtCommCodeList.FindALLSliced(startRecord, maxRecord,typeName);
            this.grdList.DataSource = dataSource;
            this.grdList.DataBind();

        }

        /// <summary>
        /// Binds the ASP net pager.(绑定分页控件)
        /// </summary>
        private void BindAspNetPager()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            string typeName = this.txtTypeCode.Text.Trim();
            int RecordCount = BKtCommCodeList.GetCount(typeName);
            aspNetPager.RecordCount = RecordCount;
            aspNetPager.CustomInfoHTML = string.Format(AspNetCustomInfoHTML, aspNetPager.PageCount);
        }

        /// <summary>
        /// Handles the PageChanged event of the AspNetPager1 control.(列表改变时候)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindAspNetPager();
            BinDate();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.txtcode.ReadOnly = false;
            Session["Action"] = "Insert";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindAspNetPager();
            BinDate();
        }
        //代码编码有效性验证
        public bool CodeValidate()
        {
            string strValidate = BKtCommCodeList.CodeValidate(this.txtcode.Text.Trim());
            if (strValidate != "success")
            {
                string strMsg = strValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        //中文名称有效性验证
        public bool CNameValidate()
        {
            string cname=this.txtCName.Text.Trim();
            int typecode=Int32.Parse(this.ddlTypecode.SelectedValue);
            string strCNameValidate = BKtCommCodeList.CNameValidate(cname,typecode);
            if (strCNameValidate != "success")
            {
                string strMsg = strCNameValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        //英文名称有效性验证
        public bool ENameValidate()
        {
            string strENameValidate = BKtCommCodeList.ENameValidate(this.txtEName.Text.Trim());
            if (strENameValidate != "success")
            {
                string strMsg = strENameValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            ClearPage();
        }

        /// <summary>
        /// 点击取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["Action"] != null)
            {
                if (Session["Action"].ToString() == "Insert")
                {
                    ClearPage();
                }
                else
                {
                    if (Session["Code"] != null)
                    {
                        InItDate(Int32.Parse(Session["Code"].ToString()));
                    }
                }
            }
        } 
    }
}
